home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_074 / less / position.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  106 lines

  1. /*
  2.  * Routines dealing with the "position" table.
  3.  * This is a table which tells the position (in the input file) of the
  4.  * first char on each currently displayed line.
  5.  *
  6.  * {{ The position table is scrolled by moving all the entries.
  7.  *    Would be better to have a circular table 
  8.  *    and just change a couple of pointers. }}
  9.  */
  10.  
  11. #include "less.h"
  12. #include "position.h"
  13.  
  14. #define NPOS    100             /* {{ sc_height must be less than NPOS }} */
  15. static POSITION table[NPOS];    /* The position table */
  16.  
  17. extern int sc_width, sc_height;
  18.  
  19. /*
  20.  * Return the position of one of:
  21.  *      the top (first) line on the screen
  22.  *      the second line on the screen
  23.  *      the bottom line on the screen
  24.  *      the line after the bottom line on the screen
  25.  */
  26.     public POSITION
  27. position(where)
  28.     int where;
  29. {
  30.     switch (where)
  31.     {
  32.     case BOTTOM:
  33.         where = sc_height - 2;
  34.         break;
  35.     case BOTTOM_PLUS_ONE:
  36.         where = sc_height - 1;
  37.         break;
  38.     }
  39.     return (table[where]);
  40. }
  41.  
  42. /*
  43.  * Add a new file position to the bottom of the position table.
  44.  */
  45.     public void
  46. add_forw_pos(pos)
  47.     POSITION pos;
  48. {
  49.     register int i;
  50.  
  51.     /*
  52.      * Scroll the position table up.
  53.      */
  54.     for (i = 1;  i < sc_height;  i++)
  55.         table[i-1] = table[i];
  56.     table[sc_height - 1] = pos;
  57. }
  58.  
  59. /*
  60.  * Add a new file position to the top of the position table.
  61.  */
  62.     public void
  63. add_back_pos(pos)
  64.     POSITION pos;
  65. {
  66.     register int i;
  67.  
  68.     /*
  69.      * Scroll the position table down.
  70.      */
  71.     for (i = sc_height - 1;  i > 0;  i--)
  72.         table[i] = table[i-1];
  73.     table[0] = pos;
  74. }
  75.  
  76. /*
  77.  * Initialize the position table, done whenever we clear the screen.
  78.  */
  79.     public void
  80. pos_clear()
  81. {
  82.     register int i;
  83.  
  84.     for (i = 0;  i < sc_height;  i++)
  85.         table[i] = NULL_POSITION;
  86. }
  87.  
  88. /*
  89.  * See if the byte at a specified position is currently on the screen.
  90.  * Check the position table to see if the position falls within its range.
  91.  * Return the position table entry if found, -1 if not.
  92.  */
  93.     public int
  94. onscreen(pos)
  95.     POSITION pos;
  96. {
  97.     register int i;
  98.  
  99.     if (pos < table[0])
  100.         return (-1);
  101.     for (i = 1;  i < sc_height;  i++)
  102.         if (pos < table[i])
  103.             return (i-1);
  104.     return (-1);
  105. }
  106.